package com.ruoyi.framework.utils;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class AliYunSendSmsUtil {


    private static String aliAccessKeyId;

    @Value("${sms.ali.aliAccessKeyId}")
    public void setAliAccessKeyId(String aliAccessKeyId) {
        this.aliAccessKeyId= aliAccessKeyId;
    }


    private static String aliAccessKeySecret;

    @Value("${sms.ali.aliAccessKeySecret}")
    public void setAliAccessKeySecret(String aliAccessKeySecret) {
        this.aliAccessKeySecret= aliAccessKeySecret;
    }


    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }



    public static void SendSms(QueryCustomerVo customerVo) throws Exception {
        List<String> args = new ArrayList<>();
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。

        Client client = AliYunSendSmsUtil.createClient(aliAccessKeyId, aliAccessKeySecret);
        SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName("阿里云短信测试")
                .setTemplateCode("SMS_154950909")
                .setPhoneNumbers(customerVo.getContactPhone())
                .setTemplateParam("{\"code\":\""+ customerVo.getContactCode() +"\"}");
       RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            client.sendSmsWithOptions(sendSmsRequest, runtime);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }


}



